package com.awesome.socketdemo.udp;

import com.teeim.ticommon.init.TiCommon;
import com.teeim.ticommon.ticleaner.TiCleaner;
import com.teeim.ticommon.ticonnection.TiConnection;
import com.teeim.ticommon.ticonnection.TiEventSocketAccept;
import com.teeim.ticommon.ticonnection.TiEventSocketConnect;
import com.teeim.ticommon.ticonnection.TiUdpSocket;
import com.teeim.ticommon.timessage.*;
import com.teeim.ticommon.titrace.TiTraceLevel;
import com.teeim.ticommon.titransaction.TiEventTransaction;
import com.teeim.ticommon.titransaction.TiTransaction;
import com.teeim.ticommon.titransaction.TiTransactionReceived;

public class UdpServer {

    private interface CallBack {
        void back();
    }

    private static void sendResponse(String ip, CallBack callBack) {
        TiUdpSocket socket = new TiUdpSocket(2, 32768, 30);
        socket.connect(ip, 8889, new TiEventSocketConnect() {
            @Override
            public void connected(TiConnection conn) {
                TiRequest req = new TiRequest(TiRequestMethod.Data);
                req.addHeader(new TiHeader(TiHeaderType.Event, 0xFF));
                TiTransaction trans = conn.createTransaction(req);
                trans.setEvent(new TiEventTransaction() {
                    @Override
                    public void responseReceived(TiTransaction tiTransaction, TiResponse rsp) {
                        conn.close();
                        socket.dispose();
                        callBack.back();
                    }

                    @Override
                    public void timeOut(TiTransaction tiTransaction) {
                        System.err.println("Send response timed out.");
                        socket.dispose();
                    }
                });
                trans.sendRequest();
            }

            @Override
            public void connectFailed() {
                System.err.println("Send response connect failed!");
            }
        });
    }

    public static void main(String[] args) {
        TiCommon.initialize(TiTraceLevel.Debug, 20);
        TiUdpSocket socket = new TiUdpSocket(2, 32768, 30);
        socket.setEvent(new TiEventSocketAccept() {
            @Override
            public void accept(TiConnection conn) {
                conn.setEvent(new TiTransactionReceived() {
                    @Override
                    public void transactionCreated(TiConnection conn, TiTransaction trans) {
                        System.out.println("from = " + conn.getRemoteAddress().getAddress().getHostAddress());
                        sendResponse(conn.getRemoteAddress().getAddress().getHostAddress(), new CallBack() {
                            @Override
                            public void back() {
                                conn.close();
                                socket.dispose();
                                TiCleaner.dispose();
                            }
                        });
                    }

                    @Override
                    public void disconnected(TiConnection conn) {
                        System.out.println("Listen disconnected.......");
                        socket.dispose();
                        TiCleaner.dispose();
                    }
                });
            }
        });
        socket.listen(8888);
    }
}
